import Sprite from "../base/Sprite.js";

class Birds extends Sprite {
  constructor() {
    const image = Sprite.getImage("birds")
    super(
      image,
      9, 10, 34, 24,
      window.innerWidth / 4, window.innerHeight / 2, 34, 24
    )
    this.count = 0
    this.speed = 0.2
    this.time = 0
    this.a
    this.g = 0.98 / 2.5
    this.birdsY = window.innerHeight/2
    this.y = window.innerHeight/2
    this.index = [9, 34 + 9 + 18, 34 + 9 + 18 + 34 + 18 + 1]
  }
  draw() {
    
    const offsetY = (this.g * this.time * (this.time-25)) / 2
    
    this.birdsY = this.y+offsetY
    // this.y +=1
    // 如果用y+=1 是累加相当于1+=2 3+=3 
    // 而用birdsY是每次offsetY更新都会重新计算birdsY的值1+=2 1+=3
    // console.log("this.y----"+this.birdsY)
    // console.log("this.offsetY----"+this.offsetY)
    this.count += this.speed
    this.time++
    this.a = Math.floor(this.count)
    if (this.a > 2) { this.a = 0,this.count = 0 }
    super.draw(
      this.img,
      this.index[this.a], 10, 34, 24,
      this.x,this.birdsY, 34, 24
    )
  }
}

export default Birds
